package rafael.bot;

import roshambo.Player;

public class RafaelSlave implements Player {

    private Rafael rafael;
    private int[] key = {1, 2, 3, 3, 1, 2, 3, 2, 1, 2, 1, 3, 1, 2, 2, 3, 2, 3, 3, 3, 3, 3};
    private boolean master = true;
    private int t;

    public RafaelSlave(int memory) {
        this.rafael = new Rafael(memory);
    }

    @Override
    public int play() {
        if (master) {
            if (t < key.length) {
                return key[t];
            } else {
                return 3;
            }
        }
        return rafael.play();
    }

    @Override
    public void receive(int result) {
        if ((t < key.length && result != Model.bestAnswer(key[t])) || (t >= key.length && result != 1)) {
            master = false;
        }
        t++;
        if (!master) {
            rafael.receive(result);
        }
    }

    @Override
    public void newGame(int games) {
        rafael.newGame(games);
        t = 0;
    }
}
